import { ref } from 'vue'

import { getDictMap } from '@/api/backend/dict'
import SignApi from '@/api/sign'

import '@/utils/security.js'

import IBaseObj from '#/index'

export const useDict = () => {
  const dictIdMaps: IBaseObj = {
    blood: 1001, // 血型
    age: 1002, // 年龄
    marriage: 1003, // 婚姻
    sex: 1004, // 性别
    job: 1005, // 职称
    idType: 1006, // 证件类型
    orgCls: 1007, // 机构分类
    projectPhase: 1008, // 项目阶段
    projectStatus: 1009, // 项目状态
    projectLevel: 1010, // 项目级别
    projectType: 1011, // 项目类型
    projectCls: 1012, // 项目分类
    subIdRule: 1013, // 受试者代码规则
    modality: 1014, // 检查状态
    control: 1015, // 控制
    intervention: 1016, // 干预
  }

  const dictIds: number[] = Object.keys(dictIdMaps).map((key) => dictIdMaps[key])
  const dictData = ref({})

  const getDictData = async (ids: number[] = dictIds) => {
    const ret = await getDictMap(ids)
    dictData.value = ret || []
  }

  return {
    dictIdMaps,
    getDictData,
    dictData,
  }
}

// 密码加密
export const passwordSecret = async (password: string) => {
  const RSAUtils = (window as any).RSAUtils
  const res = await SignApi.getKeyPair()
  const { exponent, ownModulus } = res
  const publicKey = RSAUtils.getKeyPair(exponent, '', ownModulus)
  let secretPassword = encodeURIComponent(password)
  secretPassword = RSAUtils.encryptedString(publicKey, secretPassword)

  return secretPassword
}
